package ace.module.account.core.impl.manager.impl;

import ace.cmp.core.constants.CoreConstant;
import ace.cmp.id.generator.api.LongIdGenerator;
import ace.cmp.json.api.JsonService;
import ace.module.account.api.enums.IamAccountEventRecordEventTypeEnum;
import ace.module.account.api.model.dto.IamAccountEventRecordDto;
import ace.module.account.core.impl.dao.entity.IamAccountEventRecord;
import ace.module.account.core.impl.manager.IamAccountEventRecordManager;
import ace.module.account.core.impl.service.model.IamAccountEventParams;
import ace.module.common.core.impl.manager.impl.AbstractEntityOfAppManager;
import java.time.Instant;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

/**
 * template author: caspar
 * template created at: 2023-12-18
 * template description: spring data jdbc querydsl rsql manager
 * generated at: 2024年1月6日 下午2:54:04
 * description:  manager implement
 */
@Slf4j
@AllArgsConstructor
@Component
public class IamAccountEventRecordManagerImpl
    extends AbstractEntityOfAppManager<IamAccountEventRecord, Long, IamAccountEventRecordDto>
    implements IamAccountEventRecordManager {

  private final LongIdGenerator idGenerator;
  private final JsonService jsonService;

  @Override
  public IamAccountEventRecord.IamAccountEventRecordBuilder from(
      Long iamAppId,
      Long accountId,
      IamAccountEventRecordEventTypeEnum eventTypeEnum,
      IamAccountEventParams accountEventParams) {
    String eventParams =
        accountEventParams == null ? StringUtils.EMPTY : jsonService.toJson(accountEventParams);
    return IamAccountEventRecord.builder()
        .id(idGenerator.get())
        .iamAppId(iamAppId)
        .iamAccountId(accountId)
        .eventParams(eventParams)
        .eventType(eventTypeEnum.getCode())
        .deleteFlag(CoreConstant.DELETE_FLAG_NOT)
        .createTime(Instant.now().toEpochMilli())
        .updateTime(Instant.now().toEpochMilli())
        .rowVersion(CoreConstant.DEFAULT_ROW_VERSION);
  }
}
